﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Caliburn.Micro;
using Agnes.Core;
using DevExpress.Xpf.Scheduler;
using DevExpress.XtraScheduler;

namespace Agnes.TaskManager
{
    /// <summary>
    /// 任务编辑界面ViewModel
    /// </summary>
    public sealed class TaskEditorViewModel : ScreenBase<Task>
    {
        /// <summary>
        /// 用于界面绑定的任务管理
        /// </summary>
        public TaskManager TaskManager { get; set; }

        /// <summary>
        /// 用于绑定循环信息页面的Appointment
        /// </summary>
        public Appointment Appointment { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="model"></param>
        /// <param name="isEdit">是否处于编辑状态</param>
        public TaskEditorViewModel(Task model, bool isEdit)
        {
            Model = model;
            DisplayName = "任务编辑框";
            CanSave = Model.IsValid;
            Model.PropertyChanged += (sender, args) =>
            {
                CanSave = Model.IsValid;
            };
            TaskManager = TaskManagerService.TaskManager;

            Appointment = new Appointment(AppointmentType.Pattern);
            Appointment.RecurrenceInfo.FromXml(Model.RecurrenceInfo);
            Appointment.RecurrenceInfo.Start = Model.StartDT;
        }

        protected override object CreateView()
        {   
            TaskEditorView res = new TaskEditorView();
            res.RecurrenceInfo = Appointment.RecurrenceInfo;
            return res;
        }

        public void Save()
        {
            Model.RecurrenceInfo = Appointment.RecurrenceInfo.ToXml();

            (TaskManagerService.TaskCalendarViewModel.GetView(TaskManagerService.TaskManager) as TaskCalendarView).ReLoaded();
            TryClose(true);
        }

        public void Cancel()
        {
            TryClose(false);
        }

        private bool _canSave = false;
        /// <summary>
        /// 是否允许保存
        /// </summary>
        public bool CanSave
        {
            get { return _canSave; }
            set
            {
                _canSave = value;
                NotifyOfPropertyChange(() => CanSave);
            }
        }

    }
}
